<?php
	
require("../includes/funciones.inc.php");
include("../includes/MPDF56/mpdf.php");

function getCurrentYear()
{
	require("../includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$strSQL = "SELECT * FROM config ORDER BY idConfig DESC";
	$rsConfig = $DB->Execute($strSQL);
	
	return $rsConfig->fields('anno');
}

function printAnilloPlata()
{
	require("../includes/constants.php");
	
	$mpdf=new mPDF();
	$mpdf->simpleTables = true;
	$mpdf->packTableData = true;
	$mpdf->cacheTables = true;
	$mpdf->defaultheaderfontsize = 10;	/* in pts */
	$mpdf->defaultheaderfontstyle = B;	/* blank, B, I, or BI */
	$mpdf->defaultheaderline = 1; 	/* 1 to include line below header/above footer */

	$mpdf->defaultfooterfontsize = 12;	/* in pts */
	$mpdf->defaultfooterfontstyle = B;	/* blank, B, I, or BI */
	$mpdf->defaultfooterline = 1; 	/* 1 to include line below header/above footer */
	
	$mpdf->SetHeader('{DATE j-m-Y}|{PAGENO}/{nb}|http://www.campanaylima.com.ar');
	$mpdf->SetFooter('{PAGENO}');	/* defines footer for Odd and Even Pages - placed at Outer margin */
	
	$html = '<h1>ANILLO DE PLATA</h1>';
		
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$strSQL = "SELECT u.idUser, u.userCompleteName, ci.paloma, SUM(puntos) sumaPuntos, COUNT(puesto) puesto
				FROM users u 
				INNER JOIN carreras_items ci ON u.idUser = ci.idUser
				INNER JOIN carreras c ON ci.idCarrera = c.idCarrera
				WHERE
				ci.puesto <> 0 AND
				c.state = '$statePublicado' AND 
				u.userState <> '$stateErase' AND LEFT(c.fecha,4) = '".getCurrentYear()."'
				GROUP BY u.idUser, u.userCompleteName, ci.paloma
				ORDER BY sumaPuntos DESC";
				
	$rsAnilloPlata = $DB->Execute($strSQL);
	//Header
	$html .= '<table id="cot_table" width="100%" border="1" cellspacing="0" cellpadding="5">';
	$html .= '	<tbody>';
	$html .= '		<tr>';
	$html .= '			<th>Palomar</th>';
	$html .= '			<th>Nombre</th>';
	$html .= '			<th>Anillo</th>';
	$html .= '			<th>Puntos</th>';
	$html .= '			<th>Puestos</th>';
	$html .= '		</tr>';		
		
	while(!$rsAnilloPlata->EOF){
		$html .= '		<tr>';
		$html .= '			<td>'.$rsAnilloPlata->fields('idUser').'</td>';
		$html .= '			<td>'.$rsAnilloPlata->fields('userCompleteName').'</td>';
		$html .= '			<td>'.$rsAnilloPlata->fields('paloma').'</td>';
		$html .= '			<td>'.$rsAnilloPlata->fields('sumaPuntos').'</td>';
		$html .= '			<td>'.$rsAnilloPlata->fields('puesto').'</td>';
		$html .= '		</tr>';	
		
		$rsAnilloPlata->MoveNext();
	}
 
	$html .= '</tbody></table>';
   
	$mpdf->WriteHTML($html);  
	$mpdf->Output();
}

function exportXLS()
{
	require("../includes/constants.php");
	require("../includes/funciones.inc.php");
	
	$arrayTituloCampos = array();
	$arrayValoresCampos = array();
	$arrayTipoDatosCampos = array();
	
	array_push($arrayTituloCampos, "Palomar");
	array_push($arrayValoresCampos, "idUser");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Usuario");
	array_push($arrayValoresCampos, "userCompleteName");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Paloma");
	array_push($arrayValoresCampos, "paloma");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Puntos");
	array_push($arrayValoresCampos, "sumaPuntos");
	array_push($arrayTipoDatosCampos, "string");
	
	array_push($arrayTituloCampos, "Puesto");
	array_push($arrayValoresCampos, "puesto");
	array_push($arrayTipoDatosCampos, "string");
	
	$strSQL = "SELECT u.idUser, u.userCompleteName, ci.paloma, SUM(puntos) sumaPuntos, COUNT(puesto) puesto
				FROM users u 
				INNER JOIN carreras_items ci ON u.idUser = ci.idUser
				INNER JOIN carreras c ON ci.idCarrera = c.idCarrera
				WHERE
				ci.puesto <> 0 AND
				c.state = '$statePublicado' AND 
				u.userState <> '$stateErase' AND LEFT(c.fecha,4) = '".getCurrentYear()."'
				GROUP BY u.idUser, u.userCompleteName, ci.paloma
				ORDER BY sumaPuntos DESC";
	
	exportarExcel($arrayTituloCampos, $arrayValoresCampos, $arrayTipoDatosCampos,  'AniloPlata', $strSQL);
}
	
switch($_REQUEST['accion'])
{
	case "print":
		printAnilloPlata();
		break;
	case "exportXLS":
		exportXLS();
		break;
}
?>